
<div class="wrapper wrapper-content page-heading animated fadeInRight">
    <div class="row">
        <div class="col-lg-9">
            <div class="tabs-container">
                <ul class="nav nav-tabs" id="tab-list-2">
                    <li class="links active" id="search-link"><a href="#search" role="tab" data-toggle="tab">Report</a></li>
                </ul>
                <div class="tab-content">
                   <div id="search" class="tab-pane active">
                        <div class="panel-body">
                            <div class="ibox-title" style="border: none;">
                              <h5 style="color: #00afef"><i>Collection Progress Status</i></h5>
                            </div>
                           <div class="ibox-content">
                           <div class="table-responsive">
                           <form id="search_bill_prog">
                           <div class="form-group">
                                <label style="text-align: right;" class="col-lg-4 control-label">Collection Year-Month</label>
                                <div class="col-lg-4 input-daterange input-group">
                                    <select name="year" class="form-control-custom">
                                      <?php
                                      $mon = array('','January','February','March','April','May','June','July','Augus','September','October','November','December');
                                      for($i = date('Y'); $i >= 2010 ; $i--):?>
                                        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                                      <?php endfor; ?>
                                    </select>
                                    <span class="input-group-addon">-</span>
                                   <select name="month" class="form-control-custom">
                                      <?php for($o = 1; $o <= 12; $o++):?>
                                        <option value="<?php echo $o < 10 ? '0'.$o : $o; ?>"><?php echo $mon[$o]; ?></option>
                                      <?php endfor; ?>
                                   </select>
                                </div>
                            </div>
                           </form>
                           </div>
                          <center><button class="btn btn-primary btn-outline btn-md m-t-n-xs" id="get-bill" type="button"><i class="fa fa-check fa-fw"></i> Get Status</button></p></center>
                           </div><!-- END OF IBOX CONTENT -->
                           <div id="result" style="display: none;">
                              <div class="ibox-content">
                                <div class="row">
                                    <div class="col-xs-9">
                                    <div class="btn-group">
                                      <a style="display:none;" id="to_pdf" href="#" class="btn btn-link" ><i class="fa fa-file-pdf-o fa-fw"></i> <small>Export as PDF</small></a>
                                      <a style="display:none;" id="to_excel" href="#" class="btn btn-link" ><i class="fa fa-file-excel-o fa-fw"></i> <small>Export as Excel</small></a>
                                      <button style="display:none;" id="to_mail" class="btn btn-link" ><i class="fa fa-envelope-o fa-fw"></i> <small>Send to Email</small></button>
                                  </div>
                                     <!--  <div class="col-xs-3">
                                        <a id="to_pdf" class="btn btn-w-m btn-outline btn-link" href="#"><i class="fa fa-file-pdf-o fa-fw"></i> <small>Export as PDF</small></a>
                                      </div>
                                      <div class="col-xs-3">
                                        <a id="to_excel" class="btn btn-w-m btn-outline btn-link" href='#'><i class="fa fa-file-excel-o fa-fw"></i> <small>Export as Excel</small></a>
                                      </div>
                                      <div class="col-xs-3">
                                        <div class="">
                                            <button class="btn btn-w-m btn-outline btn-link dropdown-toggle" id="to_mail"><i class="fa fa-envelope-o fa-fw"></i> <small>Send to Email</small></button>
                                        </div>
                                      </div> -->
                                    </div>
                                </div>
                             </div><!-- END OF IBOX CONTENT -->
                             <br>
                             <p id="search_values"></p>
                             <div class="horizontal-slide"> <!--  -->
                              <table class="dataTable table table-striped table-hover" style="width: 100%;">
                                <thead>
                                  <tr>
                                    <th data-sort-ignore="true" data-sort-initial="true" data-type="alpha">Zone 1</th>
                                    <!-- <th data-sort-ignore="true" style="display:none;">Zone 2</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 3</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 4</th>
                                    <th data-sort-ignore="true" style="display:none;">Zone 5</th> -->
                                    <th data-type="numeric">Active Customers</th>
                                    <th data-type="numeric">Bills Count</th>
                                    <th data-type="numeric">Bills Amount</th>
                                    <th data-type="numeric">Bills Paid</th>
                                    <th data-type="numeric">Pending Payment</th>
                                    <th data-type="numeric">Collected Amount</th>
                                    <th data-type="numeric">% Bills Paid</th>
                                    <th data-type="numeric">% Amount Collected</th>
                                  </tr>
                                </thead>
                                <tbody id="bill_table">
                                </tbody>
                                <tbody id="bill_table_foot">
                                </tbody>
                              </table>
                              </div>
                           </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div><!-- END of class row-->
</div>
<script>
      $(document).ready(function(){
        $('#search_bill_prog').keydown(function (e) {
          if (e.which == 13) {
            $('#get-bill').trigger('click');
            return false;
          }
        });

        $(document).on('click','.open_pending',function(){
          console.log($(this).attr('date'));
          console.log($(this).attr('zone'));
          var title = 'Pending List';
          var url = '<?php echo base_url($this->session->userdata('forajax')); ?>/get_pend?date='+$(this).attr('date')+'&zone='+$(this).attr('zone');
          window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no');
          //window.open("<?php echo base_url($this->session->userdata('forajax')); ?>/pending_list","Send Email", "height=800,width=500");
        });

        $('#get-bill').click(function(){
          $('#email_html').hide();
          $.ajax({
            url: "<?php echo base_url($this->session->userdata('forajax')); ?>/get_status",
            type: 'POST',
            dataType: 'json',
            data: $('#search_bill_prog').serialize(),
            success: function(data){
              $('#get-bill').removeAttr('disabled');
              var tmp = "";
              var tmp2 = "";
              var count = data.length - 1;
              var t_bill_amt = 0;
              var t_custs_count = 0;
              var t_bill_count = 0;
              var t_pend_count = 0;
              var t_pay_count = 0;
              var t_percent_avrg = 0;
              var t_pay_amt = 0;
              var t_amt_collected = 0;
              var t_bill_paid = 0;
              console.log(data.length);
              if(data.length == 0){
                $('#bill_table_foot').html('');
                 $('#bill_table').html('<tr><td colspan="10" align="center">No Result</td></tr>');
                 $('#to_excel').hide();
                 $('#to_pdf').hide();
                 $('#to_mail').hide();
                 tmp = "";
                 tmp2 = "";
                 count = data.length - 1;
                 t_bill_amt = 0;
                 t_custs_count = 0;
                 t_bill_count = 0;
                 t_pend_count = 0;
                 t_pay_count = 0;
                 t_percent_avrg = 0;
                 t_pay_amt = 0;
                 t_amt_collected = 0;
                 t_bill_paid = 0;
              }else{
                $.each(data,function(i,item){
                  //<td style="display:none;">'+ data[i].zone_2 +'</td><td style="display:none;">'+ data[i].zone_3 +'</td><td style="display:none;">'+ data[i].zone_4 +'</td><td style="display:none;">'+ data[i].zone_5 +'</td>
                  tmp += '<tr><td>'+ data[i].area +'</td><td align="right">'+ data[i].custs_count +'</td><td align="right">'+ data[i].bill_count +'</td><td align="right">'+ parseFloat(data[i].bill_amt).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,") +'</td><td align="right">'+ data[i].pay_count +'</td><td align="right"><a style="padding:0px;color: #4B67A7;" href="#" date="'+$('select[name=year]').val()+'-'+$('select[name=month]').val()+'" zone="'+data[i].area+'" class="btn btn-link open_pending">'+ data[i].pending_pay +'</a></td><td align="right">'+ parseFloat(data[i].pay_amt).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,") +'</td><td align="right">'+ parseFloat( data[i].count_percent).toFixed(2) +'%</td><td align="right">'+ parseFloat(data[i].amt_percent).toFixed(2) +'%</td></tr>'
                  t_bill_amt = t_bill_amt + parseFloat(data[i].bill_amt);
                  t_custs_count = t_custs_count + parseFloat(data[i].custs_count);
                  t_bill_count = t_bill_count + parseFloat(data[i].bill_count);
                  t_pend_count = t_pend_count + parseFloat(data[i].pending_pay);
                  t_pay_amt = t_pay_amt + parseFloat(data[i].pay_amt);
                  t_pay_count = t_pay_count + parseFloat(data[i].pay_count);
                });
                t_percent_avrg = parseFloat(t_bill_count) * 100 / parseFloat(t_custs_count);
                t_amt_collected = parseFloat(t_pay_amt) * 100 / parseFloat(t_bill_amt);
                t_bill_paid = parseFloat(t_pay_count) * 100 / t_bill_count;
                tmp2 = '<tr>'
                      +'<td></td>'
                      +'<td align="right"><b>'+ t_custs_count +'</b></td>'
                      +'<td align="right"><b>'+ t_bill_count +'</b></td>'
                      +'<td align="right"><b>'+ t_bill_amt.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</b></td>'
                      +'<td align="right"><b>'+ t_pay_count +' </b></td>'
                      +'<td align="right"><b>'+ t_pend_count +'</b></td>'
                      +'<td align="right"><b>'+ t_pay_amt.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')  +'</b></td>'
                      +'<td style="display:none;" align="right">'+ parseFloat(data[count].bill_amt).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') +'</td>'
                      +'<td align="right"><b>'+ t_bill_paid.toFixed(2) +' %</b></td>'
                      +'<td align="right"><b>'+ t_amt_collected.toFixed(2) +' %</b></td></tr>'
                $('#bill_table').html(tmp);//$('#bill_table').html(tmp).trigger('footable_redraw');
                $('#bill_table_foot').html(tmp2);
                $('.dataTable').DataTable({
                    "retrieve": true,
                    "scrollX": true,
                    "dom": '<"toolbar">Tgt',
                    "tableTools": {
                        "aButtons": [{
                                        "sExtends": "copy",
                                        "sButtonText": "Copy to clipboard"
                                    },
                                    {
                                        "sExtends": "xls",
                                        "sButtonText": "Export to Excel"
                                    }],
                        "sSwfPath": "<?php echo base_url() ?>assets/js/plugins/dataTables/swf/copy_csv_xls_pdf.swf"
                    }
                });
                 $('div.toolbar').html('Collection Progress Year-Month <strong>' + $('select[name=year]').val() + '</strong>-<strong>' + $('select[name=month]').val());
               // $('#bill_table_foot').html(tmp2);
                $('#to_excel').attr('href','<?php echo base_url($this->session->userdata("forajax")); ?>/gen_excel?year='+$('select[name=year]').val()+'&month='+$('select[name=month]').val());
                $('#to_pdf').attr('href','<?php echo base_url($this->session->userdata("forajax")); ?>/gen_pdf?year='+$('select[name=year]').val()+'&month='+$('select[name=month]').val());
               // $('#to_excel').show();
               // $('#to_pdf').show();
                //$('#to_mail').show();
              }
              $('#result').show();
            },
            beforeSend: function(){
              $('#get-bill').attr('disabled',true);
              $('#result').hide();
            },
            complete: function(){
               $('#get-bill').removeAttr('disabled');
               $('#result').show();
            }
          });
        });
  
      });
</script>